//
//  HomeViewController.m
//  LiveEducation
//
//  Created by Liu,Qingcan on 2017/4/11.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "HomeViewController.h"
#import "RoomCategoryController.h"
#import "CategoryModel.h"

@interface HomeViewController ()

@property (nonatomic,strong)NSMutableArray <NSNumber *> *cateIdArray;

@end

@implementation HomeViewController

#pragma mark lifecircle

//-(void)loadView{
//    
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setDatas];
    [self setupSegmentView];
    
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = NO;
    //    [self.tabBarController hidesBottomBarWhenPushed];
    //    [self.tabBarController setHidesBottomBarWhenPushed:NO];
    
    //    self.navigationController.navigationBar.hidden = YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark private

- (void)setupSegmentView{
    self.title = @"首页";
    [self setSegTitleView];
    [self setupSegView];
    [self.view bringSubviewToFront:self.segTitleView];
    [self.segView showVCWithIndex:0 outsideVC:self];

}


//网络加载数据 验证步骤，应该预先加载数据
- (void)setDatas{
    //1. 从数据库取数据
    CategoryModel *model = [[CategoryModel alloc]init];
    NSArray *allCate =[model selectAllData];
    if (allCate.count == 0) {
        self.titleArray = [[NSMutableArray alloc]initWithObjects:@"公务员", nil];//,@"公务",@"考研",@"话剧",@"教师",@"警察",@"三支一扶",@"村官"
        [self.cateIdArray addObject:[NSNumber numberWithInt:1]];
    }else{
        for(LECategoryItem *cate in allCate){
            [self.titleArray addObject:cate.name];
            [self.cateIdArray addObject:[NSNumber numberWithInt:cate.nid]];
        }
    }
    
   
    
    
    
    
    CGRect collectviewFrame = CGRectMake(0, 0, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height - self.navigationController.tabBarController.tabBar.bounds.size.height - NavBarHeight - 48);
    for (int dex = 0; dex < self.titleArray.count; dex ++) {
        RoomCategoryController *roomShowVC = [RoomCategoryController initWithCategoryName:self.titleArray[dex] categoryId:self.cateIdArray[dex].integerValue frame:collectviewFrame];
        
//        RoomCategoryController *roomShowVC = [RoomCategoryController initWithCategoryName:self.titleArray[dex] categoryId:self.cateIdArray[dex].integerValue frame:collectviewFrame];
        
        [self.vcArray addObject:roomShowVC];
        [self addChildViewController:roomShowVC];
    }
    
    
    
    // 2.网络异步请求网络数据，以更新数据库
    
}

- (void)setSegTitleView{
    self.segTitleView = [WLSegmentTitleView initWithFrame:CGRectMake(0, NavBarHeight, self.view.frame.size.width, 48) delegate:self childTitles:self.titleArray isScaleTitleText:YES isScroll:YES];
    self.segTitleView.backgroundColor = [UIColor whiteColor];
    self.segTitleView.titleSelectColor = [UIColor orangeColor];
    self.segTitleView.titleColor = [UIColor blackColor];
    //[UIColor colorWithRed:85/255.f green:210/255.f blue:208/255.f alpha:1];
    self.segTitleView.segmentStyle = WLSegmentTitleStyleBottomIndicatorLine;
    self.segTitleView.indicatorColor = [UIColor orangeColor];
    self.segTitleView.segmentBackImage = [UIImage imageNamed:@"bantouming3"];
//    self.segTitleView.backgroundColor = [UIColor colorWithRed:127/255.f green:127/255.f blue:127/255.f alpha:0.5];//44 42 47
    
    self.segTitleView.titleFontGradualChange = YES;
    [self.view addSubview:self.segTitleView];

}

- (void)setupSegView{
    self.segView = [WLSegmentView segmentViewWithFrame:CGRectMake(0, NavBarHeight + self.segTitleView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - (NavBarHeight + self.segTitleView.frame.size.height))];
    self.segView.vcArray = self.vcArray;
    self.segView.delegate = self;
    [self.view addSubview:self.segView];
}

#pragma mark getter & setter


- (NSMutableArray <NSNumber *> *)cateIdArray{
    if (!_cateIdArray) {
        _cateIdArray = [[NSMutableArray alloc]init];
    }
    return _cateIdArray;
}

@end
